#include "Variable_Function.h"
//#include <ipu.h>
#include <fcntl.h>
#include <errno.h>
//#include <sys/mman.h>
#include <stdio.h>
//#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

//#include <sys/socket.h>
//#include <netinet/in.h>
//#include <arpa/inet.h>

#include <sys/time.h> 
#include <sys/types.h> 
//#include <sys/socket.h>
//#include <sys/select.h>
//#include <unistd.h>

//#include "qsImgLib.h"
//#include "RunLength_Algorithm.h"
//#include "Communication.h"

#include <ncc/NCC_Track.h>

/*********************跟踪相关变量*NCC*********************/  
int dis_w, dis_h;
//extern void Init_Addr(void);
extern int TmpH;
extern int TmpW;
extern int gSubImageWidth;
extern int gSubImageHeight;
extern unsigned short tar_x,tar_y;
extern unsigned char *gSubImageData;
//static float f32_NccRatio = 0.01f;
int Cnt_Track = 0;
extern unsigned char *Template;
extern unsigned char *Template2;
extern unsigned char *show_16x16;
extern unsigned char *scale_2_moban;
unsigned char mubiaodiushi = 1;

//**********************跟踪图像****************************
unsigned char QS_img_8u3_rgb888[960*544*3]={0};
unsigned char gSubImageData_QS_544[960*544*3/2]={0};
//QS跟踪器  _目标框*******************************************
//QSRECT rcInit;
//**********************IR****NCC***************************
//unsigned char gSubImageData_IR_512[640*512]={0};
extern unsigned char* src_img;
//**********************************************************
//extern short IR_BoMen_x0;	   //红外波门中心水平坐标。
//extern short IR_BoMen_y0;	   //红外波门中心垂直坐标。

unsigned short CCD_Target_x = 0,CCD_Target_y = 0;  //CCD高清 目标位置
unsigned short IR_Target_x = 0,IR_Target_y = 0;  //IR标清 目标位置

int scale_2_TmpW=32,scale_2_TmpH=32;//模板宽度、模板高度
//**********************************************************


int init()
{
    Init_Addr();
}

int NNC_IMAGE_TRACK(unsigned char* srcimg, int Frame_NUM , int mubiaoX, int mubiaoY)
{
	int x,y;
    short Picture_Width  = 2048;
    short Picture_Height = 2048;

    gSubImageData = srcimg;

	
    if(Frame_NUM == 1)
	{
        tar_x = mubiaoX;
        tar_y = mubiaoY;              //正常情况

        //扣出模板
//        for(x=0; x<TmpW; x++)
//        {
//            for(y=0; y<TmpH; y++)
//            {
//                Template[x*TmpH+y] = gSubImageData[(tar_y-TmpW/2+x)*Picture_Width+(tar_x-TmpW/2+y)];
//            }
//        }
        achieveTemplate(gSubImageData, Template, 2048, 2048, TmpW, TmpH, tar_x, tar_y);
//        MobanPixelZoom(Template,  //输入模板源
//                scale_2_moban,    //输出
//                TmpW,             //输入模板宽16
//                TmpH,             //输入模板高16
//                scale_2_TmpW,     //输出模板宽32
//                scale_2_TmpH);    //输出模板高32
//        ImageScale_x2(Template, scale_2_moban, TmpW, TmpH);
        myResize(Template, scale_2_moban, TmpW, TmpH, scale_2_TmpW, scale_2_TmpH);
		//960x544 下全屏重捡，可能在边界处存在段错误，有可能部分参数计算超出了！
		TrackLevel(0);//判断跟踪器强度: 0	丢失目标局部重检; 1	丢失目标全屏重检; 2	更难丢失目标条件
		TrackInit(&tar_x,&tar_y);
		mubiaodiushi = 0;                                  //初始跟踪 应为跟上目标
	}

	if(Frame_NUM >= 1)
	{
		float f32_nccRatio = 0;
		gSubImageWidth  = Picture_Width;
		gSubImageHeight = Picture_Height;
		f32_nccRatio = TrackTarget(gSubImageData,Template,Template2,gSubImageWidth,gSubImageHeight,TmpW,TmpH,&tar_x,&tar_y);	
		if(f32_nccRatio >= 0.7)
			mubiaodiushi = 0;
		else
			mubiaodiushi = 1;
        qDebug("**********mubiaodiushi = %d*******\n", mubiaodiushi);
        if(mubiaodiushi == 0)
        {
            qDebug("x:%d y:%d", tar_x, tar_y);
        }
        achieveTemplate(gSubImageData, show_16x16, 2048, 2048, TmpW, TmpW, tar_x, tar_y);
	}

}




